//
//  CustomScrollView.m

//
//  Created by Lucien Wei on 4/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#define kEdgeWidth 70.0
#define kCardsMessageHeight 20.0
#define kScrollHeaderHeight 40.0
#define kUnitWidth 56.0
#import "CustomScrollView.h"

@interface CustomScrollView ()
{
    NSInteger offSetCount;
    NSInteger imageCount;
    UIButton  *leftButton;
    UIButton  *rightButton;
}
@property (nonatomic,assign) NSInteger imageCount;
@property (nonatomic, retain) UIButton  *leftButton;
@property (nonatomic, retain) UIButton  *rightButton;

- (void)buttonPressed:(UIButton*)button;
- (void)imagesPressed:(UIButton*)button;
- (void)sideButtonEnabled;
@end


@implementation CustomScrollView
@synthesize delegate,leftButton,rightButton;
@synthesize scrollView;
@synthesize imageCount;

#pragma mark -

- (void)dealloc
{
    self.delegate = nil;
    [leftButton release];
    [rightButton release];
    [scrollView release];
	[super dealloc];
}

- (id)initWithFrame:(CGRect)frame scrollViewState:(UIScrollViewState)scrollState
{
    self = [super initWithFrame:frame];
    if (self) {
       
        float centerY;
        float scrollHeaderHeight;
        
        if (scrollState == UIScrollViewHome) {
            self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
            
            UIImageView *attackImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
            [attackImageView setFrame:CGRectMake(105.0, 3.0, 26.0, 27.0)];
            [self addSubview:attackImageView];
            [attackImageView release];
            UIImageView *defenseImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
            [defenseImageView setFrame:CGRectMake(211.0, 3.0, 26.0, 27.0)];
            [self addSubview:defenseImageView];
            [defenseImageView release];
        } else if (scrollState == UIScrollViewFormation){
            self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];

        }
        centerY = self.center.y + kScrollHeaderHeight/2;
        scrollHeaderHeight = kScrollHeaderHeight;
        
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width-kEdgeWidth,self.frame.size.height-scrollHeaderHeight)];
        scrollView.center = CGPointMake(self.center.x, centerY);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        //scrollView.pagingEnabled = YES;
        //scrollView.bounces = NO;
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.delegate = self;
        [self addSubview:scrollView];
        
        leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftButton setFrame:CGRectMake(0.0, 0.0, kEdgeWidth/2, self.frame.size.height-scrollHeaderHeight)];
        [leftButton setImage:[UIImage imageNamed:@"left_arrow.png"] forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        leftButton.tag = 31;
        [leftButton setCenter:CGPointMake(kEdgeWidth/4, self.scrollView.center.y-5.0)];
        [self addSubview:leftButton];
        rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightButton setFrame:CGRectMake(0.0, 0.0, kEdgeWidth/2, self.frame.size.height-scrollHeaderHeight)];
        [rightButton setImage:[UIImage imageNamed:@"right_arrow.png"] forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        rightButton.tag =32;
        [rightButton setCenter:CGPointMake(self.frame.size.width-kEdgeWidth/4, self.scrollView.center.y-5.0)];
        [self addSubview:rightButton];
    }
    return self;
}

- (void)addImagesInScrollView:(NSArray *)array Width:(float)width
{
    float scrollViewWidth;
    for (UIView *subView in [self.scrollView subviews]) {
        [subView removeFromSuperview];
    }
    self.imageCount = array.count;
    if (array.count ==0 ) {
        self.imageCount = 10;        
    }
    for (int i=0; i<self.imageCount; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(0.0, 0.0, width, width)];
        [button setCenter:CGPointMake(10.0+(10.0+width)*i+width/2, self.scrollView.frame.size.height/2-5.0)];
        button.showsTouchWhenHighlighted = YES;
        if (array.count >0 ) {

        }
        button.tag = 3000+i;        
        [button setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(imagesPressed:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:button];
    }
    NSString *delegateString = [NSString stringWithFormat:@"%@",self.delegate];
    if ([delegateString rangeOfString:@"HomeViewController"].location != NSNotFound) {
        scrollViewWidth = (10.0+width)*self.imageCount;
    } else {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(10.0+(10.0+width)*self.imageCount, 0.0, width, width)];
        [button setCenter:CGPointMake(10.0+(10.0+width)*self.imageCount+width/2, self.scrollView.frame.size.height/2-5.0)];

        button.showsTouchWhenHighlighted = YES;
        button.tag = 113;
        [button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
        [button setImage:nil forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(imagesPressed:) forControlEvents:UIControlEventTouchUpInside];
        [scrollView addSubview:button];
        scrollViewWidth = (10.0+width)*(self.imageCount+1);
    }
    if (scrollViewWidth<320.0) {
        scrollViewWidth = 320.0;
    }
    [scrollView setContentSize:CGSizeMake(scrollViewWidth, scrollView.frame.size.height)];
    
    [self sideButtonEnabled];
}
//选中两侧按钮后 scrollView 滑到到指定的位置
- (void)buttonPressed:(UIButton*)button
{
    if (button.tag == 31) {
        offSetCount-=1;    
    } else if (button.tag == 32) {
        offSetCount+=1;
    }
    [self sideButtonEnabled];
    
    CGPoint bottomOffset = CGPointMake(kUnitWidth * offSetCount,0.0);
    [self.scrollView setContentOffset:bottomOffset animated:YES];
}

//点击image后执行delegate方法
- (void)imagesPressed:(UIButton*)button
{
    if (button.frame.origin.x < 6.0) {
        offSetCount = 0;
    } else if (button.frame.origin.x>10.0 && self.scrollView.contentSize.width-button.frame.origin.x > kUnitWidth*2) {       
        offSetCount = (uint)(button.frame.origin.x-10.0)/kUnitWidth -1;
    } else if (self.scrollView.contentSize.width-button.frame.origin.x > kUnitWidth) {
        offSetCount = (uint)(button.frame.origin.x-10.0)/kUnitWidth -2;
    } else if (self.scrollView.contentSize.width-button.frame.origin.x > 0.0) {
        offSetCount = (uint)(button.frame.origin.x-10.0)/kUnitWidth -3;
    }
    
    [self sideButtonEnabled];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    CGPoint bottomOffset = CGPointMake(offSetCount*kUnitWidth,0.0);
    [self.scrollView setContentOffset:bottomOffset animated:YES];
    [UIView commitAnimations];
    UIImage *image = [UIImage imageNamed:@"image.png"];    
    if (button.imageView.image == image) {
        NSLog(@"%d",button.tag - 3000);
        [delegate scrollViewAddCardButtonPressed:button.tag-3000];
    } else if (button.tag == 113) {
        [delegate scrollViewLockButtonPressed];
    } else {
        [delegate scrollViewButtonPressed:button.tag-3000];
    }
}
//设置两侧的Button是否可以点击
- (void)sideButtonEnabled
{
    int unitCount;
    NSString *delegateString = [NSString stringWithFormat:@"%@",self.delegate];
    if ([delegateString rangeOfString:@"HomeViewController"].location != NSNotFound) {
        unitCount = self.imageCount - 5;
    } else {
        unitCount = self.imageCount - 4;
    }
    if (offSetCount<1) {
        offSetCount = 0;
        [leftButton setEnabled:NO];
        [rightButton setEnabled:YES];
    } else if (offSetCount > unitCount){
        offSetCount = unitCount + 1;
        
        [leftButton setEnabled:YES];
        [rightButton setEnabled:NO];
    } else {
        [leftButton setEnabled:YES];
        [rightButton setEnabled:YES];
    }
}

#pragma mark - ScrollView delegate methods -
//减速停止了时执行，手触摸时执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView01;
{
    int page = (scrollView01.contentOffset.x+28.0) / kUnitWidth;
    if (offSetCount != page) {
        offSetCount = page;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.1];
        CGPoint bottomOffset = CGPointMake(kUnitWidth*offSetCount,0.0);
        [self.scrollView setContentOffset:bottomOffset animated:YES];
        [UIView commitAnimations];
    }
    [self sideButtonEnabled];
}

//完成拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView01 willDecelerate:(BOOL)decelerate;
{
    if (!decelerate) {
        int page = (scrollView01.contentOffset.x+28.0) / kUnitWidth;
        offSetCount = page;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.1];
        CGPoint bottomOffset = CGPointMake(kUnitWidth*offSetCount,0.0);
        [self.scrollView setContentOffset:bottomOffset animated:YES];
        [UIView commitAnimations];
        [self sideButtonEnabled];
    }
}

@end



